/*
   A simple singly linked list class with a nested Node class
   for convenience.  This list does not employ a dummy/sentinel
   Node at the start of the list.

   You must write the addLast and set methods for this
   class.

   --addLast places the data passed in in a new Node and places this Node
   at the end of the list

   --The set method accepts an integer index and a Comparable object and replaces
   the element at the specified index with the specified object.  If the index
   lies outside the bounds of the list, the method prints the error message
   "index outside bounds of list".  The nodes of the list are counted starting
   at 0 (just as with arrays).

   Do not add any other features or methods to this class.
*/

public class LinkedList
{
	protected static class Node
	{
		public Node _next;
		public Comparable _data;

		protected Node(Comparable data, Node next)
		{
			_next = next;
			_data = data;

		}//end constructor

		protected Node(Comparable data)
		{
			this(data, null);

		}//end constructor

	}//end inner class Node

	protected Node _head;
	protected int _size;

	private void init()
	{
		_head = null;
		_size = 0;

	}//end init method

	public LinkedList()
	{
		init();

	}//end constructor

	public void empty()
	{
		init();

	}//end empty method

	public boolean isEmpty()
	{
			return _head == null;
	}

	public int length()
	{

		return _size;
	}

	public String toString()
	{
		if (isEmpty())
			return "List is empty";
		else
		{
			String result = "";
			for (Node cur = _head; cur != null; cur = cur._next)
				result += cur._data + "\n";

			return result;

		}

	}//end toString method


//*************************************************************************
// Write your methods below
/*ans bl*/
public void addLast(Comparable data)
{
	Node newNode = new Node(data);
	if (isEmpty())
		_head = newNode;
	else
	{
		Node cur;
		for (cur = _head; cur._next != null; cur = cur._next)
			; //body
		cur._next = newNode;
	}
	_size++;

}

public void set(int index, Comparable data)
{
	if (index < 0 || index >= _size)
		System.out.println("index outside bounds of list");
	else
	{
		int i;
		Node cur;
		for (i = 0, cur = _head; i < index; ++i, cur = cur._next)
			; //body

		cur._data = data;

	}

}
/*ans ab*/


}//end class LinkedList